package cn.edu.scau.cmi.linhongyu.hibernate.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


/**
 * 简单的HibernateSessioFactory工厂类
 * 
 * 同学们自己考虑使用单例的模式
 * 
 * 
 */
public class HibernateSessionFactoryUtil {
	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;

//	sessionFactory单例
	private static SessionFactory getSessionFactory()throws HibernateException{
	    Configuration configuration =new Configuration();
	    configuration.configure();
	    serviceRegistry =new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	    return sessionFactory;
	}

	public static Session getSession(){
		 Session session=getSessionFactory().openSession();
		return session;
	}
}